package cshi.common;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;

public class ContainerTest {

	public static void main(String[] args) {
		t();

	}
	
	public static void t(){
		//asList接受一个数组或逗号分隔的列表，转换成List对象
		List<Integer> collection = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));

		Integer[] moreInts = {6,7,8};
		collection.addAll(Arrays.asList(moreInts));//addAll是成员方法
	
		Collections.addAll(collection, 9,10);//接受collection对象，把数组，或逗号分隔的列表添加进行
		print(collection);
		System.out.println("");
		doubleIterate(collection.listIterator());
		
		List<Integer> list = Arrays.asList(1,2,3);
		//list.add(4);//底层数组不允许调整大小
		
		PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
		Random random = new Random(47);
		for (int i=0; i<10; i++)
			priorityQueue.offer(random.nextInt(i+10));//用offer插入时，这个对象会被排序
		print(priorityQueue.iterator());
	}
	
	private static void print(Collection<Integer> collection) {
		for (Integer integer:collection){
			System.out.print(integer + " ");
		}
	}
	//用迭代器不用关心具体类型,Iterator只能单向移动
	private static void print(Iterator<Integer> it) {
		while (it.hasNext()){
			System.out.println(it.next() + " ");
		}
	}
	//listiterator可双向在List上迭代，还可以一开始就指定初使位置
	private static void doubleIterate(ListIterator<Integer> it){
		while (it.hasNext()){
			
		}
		while (it.hasPrevious()){
			
		}
	}
	
	ArrayList list = new ArrayList();
	List<String> strings = new ArrayList<>();//尽量创建具体类，将其转换成对应的接口
	Collection<Long> longs = new ArrayList<>();
	Set<Integer> set = new HashSet<>();//hashset用到了散列，treeset用的红黑树，linkedhashset还用到了链接来维护顺序
	
	Queue<Integer> queue = new LinkedList<>();//linkedlist实现了queue接口，可看作一种实现
}
